//
//  ZZTextField.m
//  test
//
//  Created by 朱圳 on 2017/12/11.
//  Copyright © 2017年 朱圳. All rights reserved.
//
#define padding 5
#define heightSpaceing 2
#import "ZZTextField.h"
#import "Masonry.h"

@interface ZZTextField()<UITextFieldDelegate>



//注释
@property (nonatomic,strong) UILabel *placeholderLabel;

//线
@property (nonatomic,strong) UIView *lineView;


//填充线
@property (nonatomic,strong) CALayer *lineLayer;

//移动一次
@property (nonatomic,assign) BOOL moved;



@end

@implementation ZZTextField
@dynamic text;

static const CGFloat lineWidth = 1;

-(void)awakeFromNib{
    [super awakeFromNib];
    
    _textField = [[UITextField alloc]initWithFrame:CGRectZero];
    _textField.borderStyle = UITextBorderStyleNone;
    _textField.font = [UIFont systemFontOfSize:15.f];
    _textField.delegate = self;
    _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self addSubview:_textField];
    
    _lineView = [[UIView alloc]initWithFrame:CGRectZero];
    _lineView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:_lineView];
    
    _images = [[UIImageView alloc]initWithFrame:CGRectZero];
    [self addSubview:_images];
    
    _nameLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    [self addSubview:_nameLabel];
    
}

-(instancetype)initWithFrame:(CGRect)frame{
    
    if(self = [super initWithFrame:frame]){
        
        _textField = [[UITextField alloc]initWithFrame:CGRectZero];
        _textField.borderStyle = UITextBorderStyleNone;
        _textField.font = [UIFont systemFontOfSize:15.f];
        _textField.delegate = self;
        [_textField addTarget:self action:@selector(changeText:) forControlEvents:UIControlEventEditingChanged];
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [self addSubview:_textField];
        
        _lineView = [[UIView alloc]initWithFrame:CGRectZero];
        _lineView.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:_lineView];
        
        _images = [[UIImageView alloc]initWithFrame:CGRectZero];
        [self addSubview:_images];
        
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        [self addSubview:_nameLabel];
        
    }
    return self;
}


-(void)changeText:(UITextField *)sender{
    self.text = sender.text;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    _images.frame = CGRectMake(0, 0, 20,25);
    _nameLabel.frame = CGRectMake(CGRectGetWidth(_images.frame)+2,0, 120, CGRectGetHeight(self.frame));
    CGFloat nameWith = CGRectGetWidth(_nameLabel.frame);
    _textField.frame = CGRectMake(CGRectGetWidth(_images.frame)+nameWith,0, 160, CGRectGetHeight(self.frame));
    
     _lineView.frame = CGRectMake(0, CGRectGetHeight(self.frame), CGRectGetWidth(self.frame), lineWidth);

}


-(void)setLy_placeholder:(NSString *)ly_placeholder{
    _ly_placeholder = ly_placeholder;
    
    _textField.placeholder = ly_placeholder;
    [_textField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
    
    if ([ly_placeholder isEqual: @"密码"]) {
        _textField.secureTextEntry=YES;
    }
}

-(void)set_images:(UIImageView *)images{
    
    _images = images;
    
    
}

-(void)set_nameLabel:(UILabel *)nameLabel{
    
    _nameLabel  = nameLabel;
}

-(void)setCursorColor:(UIColor *)cursorColor{
    _textField.tintColor = cursorColor;
}

-(void)setText:(NSString *)text{
    self.textField.text = text;
}

-(NSString *)text{
    return self.textField.text;
}

@end
